<?php
$parentCaption = BUtils::T('loyalty_program_lbl');
$this->pageTitle = $parentCaption;

$this->breadcrumbs = array(
	$parentCaption
);
?>
<div class="page-header position-relative">
	<div class="pull-right">
		<a class="btn btn-primary" href="<?php echo Yii::app()->createUrl('/points/to'); ?>">
			<i class="icon-credit-card bigger-120"></i>
			<?php echo BUtils::T('points_to_customer_lbl'); ?>
		</a>
		<a class="btn btn-success" href="<?php echo Yii::app()->createUrl('/points/from'); ?>">
			<i class="icon-trophy bigger-120"></i>
			<?php echo BUtils::T('points_from_customer_lbl'); ?>
		</a>
	</div>
	<h1>
		<?php echo $parentCaption; ?>
	</h1>
</div><!--/.page-header-->

<div class="row-fluid">
	<!-- Message -->
	<div class="alert alert-block alert-info">
		<button type="button" class="close" data-dismiss="alert">
			<i class="icon-remove"></i>
		</button>
		<i class="icon-info-sign"></i>
		<?php echo BUtils::T('loyalty_program_benefit_eg'); ?>
	</div>
	
	<div class="space-6"></div>
	
	<div class="row-fluid">
		<div class="span8">
			<div class="widget-box transparent">
				<div class="widget-header">
					<h3 class="lighter green"><i class="icon-trophy"></i> <?php echo $parentCaption; ?></h3>
					<div class="widget-toolbar">
						<a id="program-preview" href="<?php echo Yii::app()->createUrl('/program/preview'); ?>" target="_blank">
							<i class="icon-eye-open"></i> <?php echo BUtils::T('preview_btn'); ?>
						</a>
						<a id="program-print" href="<?php echo Yii::app()->createUrl('/program/print'); ?>" target="_blank">
							<i class="icon-print"></i> <?php echo BUtils::T('print_lbl'); ?>
						</a>
					</div>
				</div>
				<div class="widget-body">
					<div class="widget-body-inner">
						<div class="widget-main">
							<div class="row-fluid" id="rwd-100">
								<form action="<?php echo Yii::app()->baseUrl; ?>/program/add" method="post" class="program-form">
									<div class="span2">
										<input name="points" type="text" class="points span12" placeholder="?" data-rel="tooltip" title="Enter the required points" />
									</div>
									<div class="span8">
										<input name="title" type="text" class="span12" placeholder="Input the title here. e.g. Get one coffee free" />
										<input name="desc" type="text" class="span12" placeholder="You may give a description for your program..." />
									</div>
									<div class="span2">
										<button onclick="return false;" class="btn btn-warning btn-small" id="program-add">
											<i id="program-add-loader" class="icon-refresh icon-spin" style="display:none;"></i>
											<i id="program-add-plus" class="icon-plus bigger-110"></i>
											<?php echo BUtils::T('add_btn') ?>
										</button>
									</div>
									<div class="row-fluid">
										<div class="span12 errorMessage" id="program-add-error"></div>
									</div>
								</form>
							</div>
							<div id="program-add-list">
								<div class="text-center">
									<img id="program-add-list-loader" style="display:none;" src="<?php echo Yii::app()->baseUrl; ?>/img/ajax-loaders/ajax-loader-4.gif" />
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="span4">
			<div class="widget-box transparent">
				<div class="widget-header">
					<h3 class="lighter blue"><?php echo BUtils::T('loyalty_program_caption_lbl'); ?></h3>
					<div class="widget-toolbar">
						<a id="program-caption-edit" href="#edit">
							<i class="icon-edit"></i> <?php echo BUtils::T('edit_btn'); ?>
						</a>
						<a id="program-caption-save" href="#save" style="display:none;">
							<i class="icon-save"></i> <?php echo BUtils::T('save_btn'); ?>
						</a>
					</div>
				</div>
				<div class="widget-body">
					<div class="widget-body-inner">
						<div class="widget-main">
							<div id="program-caption-form"><?php echo trim($program['caption']); ?></div>
						</div>
					</div>
				</div>
			</div>
			<div class="widget-box transparent">
				<div class="widget-header">
					<h3 class="lighter blue"><?php echo BUtils::T('terms_conditions_lbl'); ?></h3>
					<div class="widget-toolbar">
						<a id="program-terms-edit" href="#edit">
							<i class="icon-edit"></i> <?php echo BUtils::T('edit_btn'); ?>
						</a>
						<a id="program-terms-save" href="#save" style="display:none;">
							<i class="icon-save"></i> <?php echo BUtils::T('save_btn'); ?>
						</a>
					</div>
				</div>
				<div class="widget-body">
					<div class="widget-body-inner">
						<div class="widget-main">
							<div id="program-terms-form"><?php echo trim($program['terms']); ?></div>
							<div class="alert alert-block">
								<button data-dismiss="alert" class="close" type="button">
									<i class="icon-remove"></i>
								</button>
								<i class="icon-info-sign"></i>
								Give terms and conditions you want tell your customers.
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
$(function() {
	//tooltips
	$('[data-rel="tooltip"]').tooltip({container:'body'});
	
	//load program list
	$.ajax({
		'type':'POST', 'url':'<?php echo Yii::app()->createUrl('/program/list'); ?>',
		'beforeSend':function(){$('#program-add-list-loader').show();},
		'success':function(data){
			$('#program-add-list-loader').show();
			$('#program-add-list').html(data);
		}
	});
	
	//add program
	$('#program-add').click(function() {
		var _form = jQuery(this).parent().parents('form');
		$.ajax({
			'type':'POST', 'url':_form.attr('action'), 'cache':false,
			'dataType':'html', 'data':_form.serialize(),
			'beforeSend':function(){beforeSubmit("#program-add");},
			'error':function(resp){errorSubmit(resp, "#program-add");},
			'success':function(data){afterSubmit(data, "#program-add");}
		});
	});
	$('#program-add').parent().parent().find('input').focus(function(){
		$(this).removeClass('error');
	}).change(function(){
		$('#program-add-error').html('');
	});
	
	//allow number input
	$('input[name="points"]').keydown(function(event) {
        // Allow: backspace, delete, tab, escape, and enter
        if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || 
             // Allow: Ctrl+A
            (event.keyCode == 65 && event.ctrlKey === true) || 
             // Allow: home, end, left, right
            (event.keyCode >= 35 && event.keyCode <= 39)) {
                 // let it happen, don't do anything
                 return;
        }
        else {
            // Ensure that it is a number and stop the keypress
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                event.preventDefault(); 
            }   
        }
    });
	//edit and save caption
	$('#program-caption-edit').click(function(ev){
		$(this).hide();
		$('#program-caption-save').show();
		var caption = $('#program-caption-form').html();
		$('#program-caption-form').html('<textarea class="span12" rows="5" >'+caption+'</textarea>');
		ev.preventDefault(); 
	});
	$('#program-caption-save').click(function(ev){
		$(this).hide();
		$('#program-caption-edit').show();
		var caption = $('#program-caption-form textarea').val();
		$('#program-caption-form').html(caption);
		//send to server
		$.post('<?php echo Yii::app()->createUrl('/program/caption'); ?>', {caption:caption});
		ev.preventDefault(); 
	});
	//edit and save terms
	$('#program-terms-edit').click(function(ev){
		$(this).hide();
		$('#program-terms-save').show();
		var terms = $('#program-terms-form').html();
		$('#program-terms-form').html('<textarea class="span12" rows="5" >'+terms+'</textarea>');
		ev.preventDefault(); 
	});
	$('#program-terms-save').click(function(ev){
		$(this).hide();
		$('#program-terms-edit').show();
		var terms = $('#program-terms-form textarea').val();
		$('#program-terms-form').html(terms);
		//send to server
		$.post('<?php echo Yii::app()->createUrl('/program/terms'); ?>', {terms:terms});
		ev.preventDefault(); 
	});
});

function beforeSubmit(eId) {
	$(eId + '-error').html('');
	$(eId).attr('disabled', true); $(eId + '-plus').hide(); $(eId + '-loader').show();
}
function errorSubmit(resp, eId) {
	$(eId).attr('disabled', false); $(eId + '-loader').hide(); $(eId + '-plus').show();
	var json = JSON.parse(resp.responseText);
	$('input[name="'+json.name+'"]').addClass('error');
	$(eId + '-error').html(json.message);
}
function afterSubmit(data, eId) {
	$(eId).attr('disabled', false); $(eId + '-loader').hide(); $(eId + '-plus').show();
	$(eId + '-list').prepend(data).animate({background:"none"}, 1000);
	$(eId + '-button').parent().find('input[type="text"]').val('');//clear old value
}

function eventPublish(e) {
	var _url = '<?php echo Yii::app()->createUrl('/program/publish'); ?>?id=' + $(e).data('id');
	$.ajax({
		'type':'GET', 'url':_url,
		'beforeSend':function(){ $(e).attr('disabled', false); },
		'error':function(resp){ $(e).attr('disabled', true); },
		'success':function(data){ $(e).hide().parent().find('.icon-ok-circle').fadeIn(); }
	});
}
function eventTrash(e) {
	var _url = '<?php echo Yii::app()->createUrl('/program/trash'); ?>?id=' + $(e).data('id');
	$.ajax({
		'type':'GET', 'url':_url,
		'beforeSend':function(){ $('#program-'+$(e).data('id')).fadeOut(); }
	});
}
</script>